<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>window_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_window_window_test.js.html">window_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.windowTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.windowTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.events&#39;);
<a name="line20"></a>goog.require(&#39;goog.string&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line23"></a>goog.require(&#39;goog.window&#39;);
<a name="line24"></a>
<a name="line25"></a>var newWin;
<a name="line26"></a>var REDIRECT_URL_PREFIX = &#39;window_test.html?runTests=&#39;;
<a name="line27"></a>var asyncTestCase =
<a name="line28"></a>    goog.testing.AsyncTestCase.createAndInstall(document.title);
<a name="line29"></a>asyncTestCase.stepTimeout = 5000;
<a name="line30"></a>
<a name="line31"></a>var WIN_LOAD_TRY_TIMEOUT = 100;
<a name="line32"></a>var MAX_WIN_LOAD_TRIES = 50; // 50x100ms = 5s waiting for window to load.
<a name="line33"></a>var winLoadCounter;
<a name="line34"></a>
<a name="line35"></a>function setUpPage() {
<a name="line36"></a>  var anchors = goog.dom.getElementsByTagNameAndClass(
<a name="line37"></a>      &#39;div&#39;, &#39;goog-like-link&#39;);
<a name="line38"></a>  for (var i = 0; i &lt; anchors.length; i++) {
<a name="line39"></a>    goog.events.listen(
<a name="line40"></a>        anchors[i], &#39;click&#39;,
<a name="line41"></a>        function(e) {
<a name="line42"></a>          goog.window.open(
<a name="line43"></a>              goog.dom.getTextContent(e.target), {&#39;noreferrer&#39;: true});
<a name="line44"></a>        });
<a name="line45"></a>  }
<a name="line46"></a>}
<a name="line47"></a>
<a name="line48"></a>
<a name="line49"></a>/**
<a name="line50"></a> * Some tests should only run locally, because they will trigger
<a name="line51"></a> * popup blockers on http urls.
<a name="line52"></a> */
<a name="line53"></a>function canOpenPopups() {
<a name="line54"></a>  // TODO(nicksantos): Fix the test runner farm.
<a name="line55"></a>  return window.location.toString().indexOf(&#39;file://&#39;) == 0;
<a name="line56"></a>}
<a name="line57"></a>
<a name="line58"></a>if (canOpenPopups()) {
<a name="line59"></a>  // To test goog.window.open we open a new window with this file again. Once
<a name="line60"></a>  // the new window gets to this point in the file it notifies the opener that
<a name="line61"></a>  // it has loaded, so that the opener knows that the new window has been
<a name="line62"></a>  // populated with properties like referrer and location.
<a name="line63"></a>  var newWinLoaded = false;
<a name="line64"></a>  if (window.opener &amp;&amp; window.opener.newWinLoaded === false) {
<a name="line65"></a>    window.opener.newWinLoaded = true;
<a name="line66"></a>  }
<a name="line67"></a>}
<a name="line68"></a>
<a name="line69"></a>function setUp() {
<a name="line70"></a>  newWinLoaded = false;
<a name="line71"></a>}
<a name="line72"></a>
<a name="line73"></a>function tearDown() {
<a name="line74"></a>  if (newWin) {
<a name="line75"></a>    newWin.close();
<a name="line76"></a>  }
<a name="line77"></a>}
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * Uses setTimeout to keep checking if a new window has been loaded, and once
<a name="line82"></a> * it has, calls the given continuation function and then calls
<a name="line83"></a> * asyncTestCase.continueTesting() to resume the flow of the test.
<a name="line84"></a> * @param {Function} continueFn Continuation function to be called when the
<a name="line85"></a> *     new window has loaded.
<a name="line86"></a> * @param {number=} opt_numTries Number of times this method has checked if
<a name="line87"></a> *     the window has loaded, to prevent getting in an endless setTimeout
<a name="line88"></a> *     loop. (Used internally, callers should omit.)
<a name="line89"></a> */
<a name="line90"></a>function continueAfterWindowLoaded(continueFn, opt_numTries) {
<a name="line91"></a>  opt_numTries = opt_numTries || 0;
<a name="line92"></a>  if (newWinLoaded) {
<a name="line93"></a>    continueFn();
<a name="line94"></a>    asyncTestCase.continueTesting();
<a name="line95"></a>  } else if (opt_numTries &gt; MAX_WIN_LOAD_TRIES) {
<a name="line96"></a>    fail(&#39;Window did not load after maximum number of checks.&#39;);
<a name="line97"></a>    asyncTestCase.continueTesting();
<a name="line98"></a>  } else {
<a name="line99"></a>    setTimeout(goog.partial(continueAfterWindowLoaded,
<a name="line100"></a>                            continueFn, ++opt_numTries),
<a name="line101"></a>               WIN_LOAD_TRY_TIMEOUT);
<a name="line102"></a>  }
<a name="line103"></a>}
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Helper to kick off a test that opens a window and checks that the referrer
<a name="line108"></a> * is hidden if requested and the url is properly encoded/decoded.
<a name="line109"></a> * @param {boolean} noreferrer Whether to test the noreferrer option.
<a name="line110"></a> * @param {string} urlParam Url param to append to the url being opened.
<a name="line111"></a> */
<a name="line112"></a>function doTestOpenWindow(noreferrer, urlParam) {
<a name="line113"></a>  if (!canOpenPopups()) {
<a name="line114"></a>    return;
<a name="line115"></a>  }
<a name="line116"></a>  newWin = goog.window.open(REDIRECT_URL_PREFIX + urlParam,
<a name="line117"></a>                            {&#39;noreferrer&#39;: noreferrer});
<a name="line118"></a>  asyncTestCase.waitForAsync(&#39;Waiting for window to open and load.&#39;);
<a name="line119"></a>  continueAfterWindowLoaded(
<a name="line120"></a>      goog.partial(continueTestOpenWindow, noreferrer, urlParam));
<a name="line121"></a>}
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/**
<a name="line125"></a> * Helper callback to do asserts after the window opens.
<a name="line126"></a> * @param {boolean} noreferrer Whether the noreferrer option is being tested.
<a name="line127"></a> * @param {string} urlParam Url param appended to the url being opened.
<a name="line128"></a> */
<a name="line129"></a>function continueTestOpenWindow(noreferrer, urlParam) {
<a name="line130"></a>  if (noreferrer) {
<a name="line131"></a>    assertEquals(&#39;Referrer should have been stripped&#39;,
<a name="line132"></a>                 &#39;&#39;, newWin.document.referrer);
<a name="line133"></a>  }
<a name="line134"></a>
<a name="line135"></a>  var newWinUrl = decodeURI(newWin.location);
<a name="line136"></a>  var expectedUrlSuffix = decodeURI(urlParam);
<a name="line137"></a>  assertTrue(&#39;New window href should have ended with &lt;&#39; + expectedUrlSuffix +
<a name="line138"></a>      &#39;&gt; but was &lt;&#39; + newWinUrl + &#39;&gt;&#39;,
<a name="line139"></a>      goog.string.endsWith(newWinUrl, expectedUrlSuffix));
<a name="line140"></a>}
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>function testOpenNotEncoded() {
<a name="line144"></a>  doTestOpenWindow(false, &#39;&quot;bogus~&quot;&#39;);
<a name="line145"></a>}
<a name="line146"></a>
<a name="line147"></a>function testOpenEncoded() {
<a name="line148"></a>  doTestOpenWindow(false, &#39;&quot;bogus%7E&quot;&#39;);
<a name="line149"></a>}
<a name="line150"></a>
<a name="line151"></a>function testOpenEncodedPercent() {
<a name="line152"></a>  // Intent of url is to pass %7E to the server, so it was encoded to %257E .
<a name="line153"></a>  doTestOpenWindow(false, &#39;&quot;bogus%257E&quot;&#39;);
<a name="line154"></a>}
<a name="line155"></a>
<a name="line156"></a>function testOpenNotEncodedHidingReferrer() {
<a name="line157"></a>  doTestOpenWindow(true, &#39;&quot;bogus~&quot;&#39;);
<a name="line158"></a>}
<a name="line159"></a>
<a name="line160"></a>function testOpenEncodedHidingReferrer() {
<a name="line161"></a>  doTestOpenWindow(true, &#39;&quot;bogus%7E&quot;&#39;);
<a name="line162"></a>}
<a name="line163"></a>
<a name="line164"></a>function testOpenEncodedPercentHidingReferrer() {
<a name="line165"></a>  // Intent of url is to pass %7E to the server, so it was encoded to %257E .
<a name="line166"></a>  doTestOpenWindow(true, &#39;&quot;bogus%257E&quot;&#39;);
<a name="line167"></a>}
<a name="line168"></a>
<a name="line169"></a>function testOpenSemicolon() {
<a name="line170"></a>  doTestOpenWindow(true, &#39;beforesemi;aftersemi&#39;);
<a name="line171"></a>}
<a name="line172"></a>
<a name="line173"></a>function testTwoSemicolons() {
<a name="line174"></a>  doTestOpenWindow(true, &#39;a;b;c&#39;);
<a name="line175"></a>}
<a name="line176"></a>
<a name="line177"></a>function testOpenAmpersand() {
<a name="line178"></a>  doTestOpenWindow(true, &#39;this&amp;that&#39;);
<a name="line179"></a>}
<a name="line180"></a>
<a name="line181"></a>function testOpenSingleQuote() {
<a name="line182"></a>  doTestOpenWindow(true, &quot;&#39;&quot;);
<a name="line183"></a>}
<a name="line184"></a>
<a name="line185"></a>function testOpenDoubleQuote() {
<a name="line186"></a>  doTestOpenWindow(true, &#39;&quot;&#39;);
<a name="line187"></a>}
<a name="line188"></a>
<a name="line189"></a>function testOpenDoubleQuote() {
<a name="line190"></a>  doTestOpenWindow(true, &#39;&lt;&#39;);
<a name="line191"></a>}
<a name="line192"></a>
<a name="line193"></a>function testOpenDoubleQuote() {
<a name="line194"></a>  doTestOpenWindow(true, &#39;&gt;&#39;);
<a name="line195"></a>}
<a name="line196"></a>
<a name="line197"></a>function testOpenBlank() {
<a name="line198"></a>  if (!canOpenPopups()) {
<a name="line199"></a>    return;
<a name="line200"></a>  }
<a name="line201"></a>  newWin = goog.window.openBlank(&#39;Loading...&#39;);
<a name="line202"></a>  asyncTestCase.waitForAsync(&#39;Waiting for temp window to open and load.&#39;);
<a name="line203"></a>  var urlParam = &#39;bogus~&#39;;
<a name="line204"></a>
<a name="line205"></a>  var continueFn = function() {
<a name="line206"></a>    newWin.location.href = REDIRECT_URL_PREFIX + urlParam;
<a name="line207"></a>    continueAfterWindowLoaded(
<a name="line208"></a>        goog.partial(continueTestOpenWindow, false, urlParam));
<a name="line209"></a>  };
<a name="line210"></a>  setTimeout(continueFn, 100);
<a name="line211"></a>}
<a name="line212"></a>
<a name="line213"></a>
<a name="line214"></a>/** @this {Element} */
<a name="line215"></a>function stripReferrer() {
<a name="line216"></a>  goog.window.open(this.href, {&#39;noreferrer&#39;: true});
<a name="line217"></a>}
</pre>


</body>
</html>
